package com.yanqu.road.logic.activity.chefduel;

import com.yanqu.road.dao.impl.activity.chefduel.ChefDuelConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2DishConfig;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2Spoon;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class ChefDuel2Config {

    private ActivityInfo activityInfo;

    private int activityId;

    RandomHelper randomHelper = new RandomHelper();

    Map<String, ActivityConfig> activityConfigMap;

    Map<Integer, ChefDuel2DishConfig> dishConfigMap;

    private Random random = new Random();


    private String CHEFDUEL2_ITEM_USE_LIMIT;
    private int CHEFDUEL2_TEAM1_PARAM;
    private int CHEFDUEL2_TEAM2_PARAM;
    private String CHEFDUEL2_TEAM_WIN_REWARD;
    private String CHEFDUEL2_TEAM_LOSE_REWARD;



    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public Map<String, ActivityConfig> getActivityConfigMap() {
        return activityConfigMap;
    }

    public ChefDuel2Config(ActivityInfo tmpActivityInfo, Map<String, ActivityConfig> tmpActivityConfigMap){
        this.activityInfo = tmpActivityInfo;
        this.activityId = activityInfo.getActivityId();
        this.activityConfigMap = tmpActivityConfigMap;
        //去数据库拿数据

        dishConfigMap = new ChefDuelConfigDaoImpl().getChefDuel2ActivityDishConfig(tmpActivityInfo.getActivityId());

        CHEFDUEL2_ITEM_USE_LIMIT = activityConfigMap.get("CHEFDUEL2_ITEM_USE_LIMIT").getValue();
        CHEFDUEL2_TEAM1_PARAM = activityConfigMap.get("CHEFDUEL2_TEAM1_PARAM").getIntValue();
        CHEFDUEL2_TEAM2_PARAM = activityConfigMap.get("CHEFDUEL2_TEAM2_PARAM").getIntValue();
        CHEFDUEL2_TEAM_WIN_REWARD = activityConfigMap.get("CHEFDUEL2_TEAM_WIN_REWARD").getValue();
        CHEFDUEL2_TEAM_LOSE_REWARD = activityConfigMap.get("CHEFDUEL2_TEAM_LOSE_REWARD").getValue();

    }

    //小当家第二阶段
    public static final String COOKBOY2_SWITCH = "1";//是否开启凭证参与
    public static final String COOKBOY2_CERTIFICATE_CONSUME = "120466=1";//参与活动需要消耗的凭证道具
    public static final String COOKBOY2_EXCHANGE_ITEM_ID = "101968";//兑换商店积分道具ID
    public static final String COOKBOY2_RESTORE_ITEM_ID = "120462";//恢复体力道具ID，恢复值在道具参数1中
    public static final String COOKBOY2_ENERGY_PARAM = "30;30";//初始体力，体力上限
    public static final String COOKBOY2_ENERGY_CD_TIME = "10";//体力恢复一点时间(秒)
    public static final String COOKBOY2_PICK_DISH_REWARDS = "110000=1;20|110001=1;30";//挑菜随机奖励列表,道具=道具数量;权重
    public static final String COOKBOY2_TRY_DISH_CONFIG = "120463=1;100;100|120464=1;200;200|120465=1;300;300";//赴宴试吃消耗道具和数量;试吃获得积分;开宴获得积分
    public static final String COOKBOY2_WORSHIP_REWARDS = "101000=200";//膜拜奖励列表
    public static final String COOKBOY2_PARTICPATE_COOKING_TIME = "60";//试吃单场时长(分)
    public static final String COOKBOY2_PARTICPATE_COOKING_TABLE = "2";//试吃每人可以开启多少道菜
    public static final String COOKBOY2_SERVE_FOOD_DAY_START = "10";
    public static final String COOKBOY2_SERVE_FOOD_DAY_END = "22";
    public static final String COOKBOY2_HORSE_RACE_LAMP_QUALITY = "4;3";//跑马灯菜品品质(;号隔开)
    public static final String COOKBOY2_ROBOT_ADD = "1000";//机器人填补座位加成积分千分比
    public static final String COOKBOY2_TO_COOKBOY3_BANK = "10";//阶段2晋级阶段3排名
    public static final String COOKBOY2_PICK_UP_ITEMID = "120462";//挑菜使用的道具


    //是否可以直接登录就参加
    public boolean isChefDuel2Able(){
        String strConfig = COOKBOY2_SWITCH;
        if(activityConfigMap.containsKey("COOKBOY2_SWITCH")){
            strConfig = activityConfigMap.get("COOKBOY2_SWITCH").getValue();
        }
        return Integer.valueOf(strConfig) == 0;
    }


    public int getChefDuel2PickUpItemId(){
        String strConfig = COOKBOY2_PICK_UP_ITEMID;
        if(activityConfigMap.containsKey("COOKBOY2_PICK_UP_ITEMID")){
            strConfig = activityConfigMap.get("COOKBOY2_PICK_UP_ITEMID").getValue();
        }
        return Integer.valueOf(strConfig);
    }

    public List<Integer> getChefDuel2SpoonIdList(){
        List<Integer> spoonIdList = new ArrayList<>();
        String strConfig = COOKBOY2_TRY_DISH_CONFIG;
        if(activityConfigMap.containsKey("COOKBOY2_TRY_DISH_CONFIG")){
            strConfig = activityConfigMap.get("COOKBOY2_TRY_DISH_CONFIG").getValue();
        }
        List<String> infoList = StringUtils.stringToStringList(strConfig,"\\|");
        for(String info : infoList){
            List<String> itemList = StringUtils.stringToStringList(info,";");
            String consume = itemList.get(0);
            int itemId = StringUtils.stringToIntegerList(consume,"=").get(0);
            spoonIdList.add(itemId);
        }
        return spoonIdList;
    }

    public ChefDuel2Spoon getChefDuel2MinSpoon(){
        String strConfig = COOKBOY2_TRY_DISH_CONFIG;
        if(activityConfigMap.containsKey("COOKBOY2_TRY_DISH_CONFIG")){
            strConfig = activityConfigMap.get("COOKBOY2_TRY_DISH_CONFIG").getValue();
        }
        List<String> infoList = StringUtils.stringToStringList(strConfig,"\\|");
        //机器人默认第一个道具赴宴处理
        for(String info : infoList){
            List<String> itemList = StringUtils.stringToStringList(info,";");
            String consume = itemList.get(0);
            int itemId = StringUtils.stringToIntegerList(consume,"=").get(0);
            ChefDuel2Spoon cookBoy2Spoon = new ChefDuel2Spoon();
            cookBoy2Spoon.setItemId(itemId);
            cookBoy2Spoon.setJoinerIntegral(Long.valueOf(itemList.get(1)));
            cookBoy2Spoon.setUserIntegral(Long.valueOf(itemList.get(2)));
            cookBoy2Spoon.setConsume(consume);
            return cookBoy2Spoon;
        }
        return null;
    }

    public List<Integer> getHouseNoticeQuality(){
        String strConfig = COOKBOY2_HORSE_RACE_LAMP_QUALITY;
        if(activityConfigMap.containsKey("COOKBOY2_HORSE_RACE_LAMP_QUALITY")){
            strConfig = activityConfigMap.get("COOKBOY2_HORSE_RACE_LAMP_QUALITY").getValue();
        }
        List<Integer> qualityList = StringUtils.stringToIntegerList(strConfig,";");
        return qualityList;
    }

    public ChefDuel2Spoon getChefDuel2Spoon(int spoonId){
        String strConfig = COOKBOY2_TRY_DISH_CONFIG;
        if(activityConfigMap.containsKey("COOKBOY2_TRY_DISH_CONFIG")){
            strConfig = activityConfigMap.get("COOKBOY2_TRY_DISH_CONFIG").getValue();
        }
        List<String> infoList = StringUtils.stringToStringList(strConfig,"\\|");
        for(String info : infoList){
            List<String> itemList = StringUtils.stringToStringList(info,";");
            String consume = itemList.get(0);
            int itemId = StringUtils.stringToIntegerList(consume,"=").get(0);
            if(itemId == spoonId){
                ChefDuel2Spoon cookBoy2Spoon = new ChefDuel2Spoon();
                cookBoy2Spoon.setItemId(itemId);
                cookBoy2Spoon.setJoinerIntegral(Long.valueOf(itemList.get(1)));
                cookBoy2Spoon.setUserIntegral(Long.valueOf(itemList.get(2)));
                cookBoy2Spoon.setConsume(consume);
                return cookBoy2Spoon;
            }
        }
        return null;
    }


    public String getChefDuel2JoinerReward(int spoonId){
        ChefDuel2Spoon cookBoy2Spoon = getChefDuel2Spoon(spoonId);
        if(cookBoy2Spoon != null){
            return getChefDuel2IntegralItemId() + ""+ cookBoy2Spoon.getJoinerIntegral();
        }
        return "";
    }

    public int getChefDuel2IntegralItemId(){
        if(activityConfigMap.containsKey("COOKBOY2_EXCHANGE_ITEM_ID")){
            return Integer.valueOf(activityConfigMap.get("COOKBOY2_EXCHANGE_ITEM_ID").getValue());
        }
        return Integer.valueOf(COOKBOY2_EXCHANGE_ITEM_ID);
    }

    public String getWorship2Reward(){
        if(activityConfigMap.containsKey("COOKBOY2_WORSHIP_REWARDS")){
            return activityConfigMap.get("COOKBOY2_WORSHIP_REWARDS").getValue();
        }
        return COOKBOY2_WORSHIP_REWARDS;
    }


    public int getChefDuel2Hp(){
        if(activityConfigMap.containsKey("COOKBOY2_ENERGY_PARAM")){
            List<Integer> configList = StringUtils.stringToIntegerList(activityConfigMap.get("COOKBOY2_ENERGY_PARAM").getValue(),";");
            return configList.get(0);
        }
        List<Integer> configList = StringUtils.stringToIntegerList(COOKBOY2_ENERGY_PARAM,";");
        return configList.get(0);
    }



    public int getChefDuel2MaxHp(){
        if(activityConfigMap.containsKey("COOKBOY2_ENERGY_PARAM")){
            List<Integer> configList = StringUtils.stringToIntegerList(activityConfigMap.get("COOKBOY2_ENERGY_PARAM").getValue(),";");
            return configList.get(1);
        }
        List<Integer> configList = StringUtils.stringToIntegerList(COOKBOY2_ENERGY_PARAM,";");
        return configList.get(1);
    }



    public int getChefDuel2RecoveryTime(){
        if(activityConfigMap.containsKey("COOKBOY2_ENERGY_CD_TIME")){
            return Integer.valueOf(activityConfigMap.get("COOKBOY2_ENERGY_CD_TIME").getValue());
        }
        return Integer.valueOf(COOKBOY2_ENERGY_CD_TIME);
    }


    public String randomPickReward(){
        String rewardStr = COOKBOY2_PICK_DISH_REWARDS;
        if(activityConfigMap.containsKey("COOKBOY2_PICK_DISH_REWARDS")){
            rewardStr =  activityConfigMap.get("COOKBOY2_PICK_DISH_REWARDS").getValue();
        }
        List<String> rewardInfoList = StringUtils.stringToStringList(rewardStr,"\\|");
        Map<String,Integer> weightRewardMap = new ConcurrentHashMap<>();
        for(String rewardInfo : rewardInfoList){
            List<String> rewardWeight = StringUtils.stringToStringList(rewardInfo,";");
            weightRewardMap.put(rewardWeight.get(0),Integer.valueOf(rewardWeight.get(1)));
        }
        int totalWeight = 0;
        for (Integer weight : weightRewardMap.values()) {
            totalWeight += weight;
        }
        int randomNum = randomHelper.next(0, totalWeight);
        for (String reward : weightRewardMap.keySet()) {
            int value = weightRewardMap.get(reward);
            if (randomNum < value) {
                return reward;
            }
            randomNum -= value;
        }
        return "";
    }

    //是不是跑马灯品质
    public boolean isHorseNoticeQuality(int dishId){
        ChefDuel2DishConfig cookBoy2DishConfig = getDishConfig(dishId);
        if(cookBoy2DishConfig == null){
            return false;
        }
        for(Integer quality : getHouseNoticeQuality()){
            if(quality == cookBoy2DishConfig.getQuality()){
                return true;
            }
        }
        return false;
    }

    public ChefDuel2DishConfig getDishConfig(int dishId){
        if(dishConfigMap.containsKey(dishId)){
            return dishConfigMap.get(dishId);
        }
        return null;
    }


    public List<String> randomPickReward(int times){
        List<String> rewardList = new ArrayList<>();
        for(int i = times;i > 0;i--){
            rewardList.add(randomPickReward());
        }
        return rewardList;
    }

    public int getServeDishStartHour(){
        if(activityConfigMap.containsKey("COOKBOY2_SERVE_FOOD_DAY_START")){
            return Integer.valueOf(activityConfigMap.get("COOKBOY2_SERVE_FOOD_DAY_START").getValue());
        }
        return Integer.valueOf(COOKBOY2_SERVE_FOOD_DAY_START);
    }

    public int getServeDishEndHour(){
        if(activityConfigMap.containsKey("COOKBOY2_SERVE_FOOD_DAY_END")){
            return Integer.valueOf(activityConfigMap.get("COOKBOY2_SERVE_FOOD_DAY_END").getValue());
        }
        return Integer.valueOf(COOKBOY2_SERVE_FOOD_DAY_END);
    }

    public String getCHEFDUEL2_ITEM_USE_LIMIT() {
        return CHEFDUEL2_ITEM_USE_LIMIT;
    }

    public int getCHEFDUEL2_TEAM1_PARAM() {
        return CHEFDUEL2_TEAM1_PARAM;
    }

    public int getCHEFDUEL2_TEAM2_PARAM() {
        return CHEFDUEL2_TEAM2_PARAM;
    }

    public String getCHEFDUEL2_TEAM_WIN_REWARD() {
        return CHEFDUEL2_TEAM_WIN_REWARD;
    }

    public String getCHEFDUEL2_TEAM_LOSE_REWARD() {
        return CHEFDUEL2_TEAM_LOSE_REWARD;
    }

    public Map<Integer, ChefDuel2DishConfig> getDishConfigMap() {
        return dishConfigMap;
    }
}
